//
//  ZHMagnifierView.m
//  test02
//
//  Created by lzh on 2020/10/16.
//  Copyright © 2020 NO. All rights reserved.
//

#import "ZHMagnifierView.h"
#define SCREEN_W [UIScreen mainScreen].bounds.size.width

@implementation ZHMagnifierView

- (void)resetRenderPoint:(CGPoint)point{
    
    if (point.x < 25) {
        self.renderPoint = CGPointMake(25, point.y);
    }else if (point.x > SCREEN_W - 25) {
        self.renderPoint = CGPointMake(SCREEN_W - 25,  point.y);

    }else {
        self.renderPoint = point;
    }
    
    [self.layer setNeedsDisplay];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initView];
    }
    return self;
}

- (void)initView{
    self.layer.cornerRadius = 10;//self.frame.size.width * 0.5;
    self.layer.masksToBounds = true;
    self.layer.delegate = self;
    self.layer.borderWidth = 2;
    self.layer.borderColor = [UIColor whiteColor].CGColor;
    self.layer.contentsScale = UIScreen.mainScreen.scale;
    self.windowLevel = UIWindowLevelStatusBar + 1;
}


- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
    CGContextTranslateCTM(ctx, self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    CGContextScaleCTM(ctx, 2, 2);
    CGContextSetLineWidth(ctx, 3);
    //  此处获取颜色要注意不然无法正常显示
    CGColorRef cgColor = [UIColor clearColor].CGColor;
    CGContextSetFillColor(ctx, CGColorGetComponents(cgColor));
    CGContextTranslateCTM(ctx, -1 * self.renderPoint.x, -1 * self.renderPoint.y);
    [self.renderView.layer renderInContext:ctx];// 把xtx 上下问渲染到.renderView.layer
}

@end
